import datamodel
import utils
import hotel_crud

import logging
from datetime import datetime

from google.appengine.ext import db

class HotelReservation():
	def create(self, dispatcher):
		logging.info("create")
		dispatcher.modelObject.put()
		return dispatcher.modelObject

	def update(self, dispatcher):
		logging.info("update")
		dispatcher.modelObject.put()
		return dispatcher.modelObject
    
   
	def delete(self, dispatcher):
		logging.info("delete")
		dispatcher.modelObject.delete()

	def get(self, dispatcher):
		logging.info("get")
		if dispatcher.request.get('key'):
			logging.info("getting reservation by key")
			reservation = self.getReservation(dispatcher.request.get('key'), dispatcher)
			return reservation
		else:
			logging.info("getting all reservations for user")
			query = datamodel.HotelReservation.all()
			query.order('name')
			if not dispatcher.isAdminData:
				query.filter('userId =', dispatcher.userId)
			return query.fetch(100)

	def decode(self, decodeType, inputStr, inputObj, dispatcher):
		myDict = utils.GqlDecoder().decode2dict(inputStr)
		if inputObj == None:
			parenthotel = hotel_crud.Hotel().getHotel(myDict["hotelKey"], dispatcher)
			reservation = datamodel.HotelReservation(parent=parenthotel)
			reservation.userId = dispatcher.userId 
			return utils.GqlDecoder().updateObj4Dict(myDict, reservation)
		else:
			return utils.GqlDecoder().decode(inputStr, inputObj)


	def encode(self, decodeType, outputObj, dispatcher):
		return utils.GqlEncoder().encode(outputObj)
